package com.jigu.admin.controller;

import com.jigu.admin.model.console.Admin;
import com.jigu.admin.model.member.Member;
import com.jigu.admin.service.UserService;
import com.jigu.core.jwt.AccessToken;
import com.jigu.core.jwt.JwtHelper;
import com.jigu.core.jwt.LoginPara;
import com.jigu.core.properties.JwtInfo;
import com.jigu.core.utils.EncryptionUtil;
import com.jigu.core.utils.ResponseMessage;
import com.jigu.core.vo.ResultStatusCode;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by toretto on 2017/6/21.
 */

@RestController
public class JwtWebTokenController {

    @Autowired
    private JwtInfo jwtInfo;

    @Autowired
    private UserService userService;

    private final static Logger logger = LoggerFactory.getLogger(JwtWebTokenController.class);

    @ApiOperation(value="输出自定义的Properties配置解析类内容")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Object getTokenInfo(){
        logger.info("获取info");
        logger.error("获取info");

        JwtInfo jwtInfos = new JwtInfo();
        jwtInfos.setClientId(jwtInfo.getClientId());
        jwtInfos.setBase64Secret(jwtInfo.getBase64Secret());
        jwtInfos.setName(jwtInfo.getName());
        jwtInfos.setExpiresSecond(jwtInfo.getExpiresSecond());
        jwtInfo = jwtInfos;
        return ResponseMessage.created(jwtInfos);
    }


    @PostMapping("oauth/token")
    public Object getAccessToken(@RequestBody LoginPara loginPara)
    {
        ResponseMessage resultMsg;
        logger.info("token");
        try
        {
            if(loginPara.getClientId() == null
                    || (loginPara.getClientId().compareTo(jwtInfo.getClientId()) != 0))
            {

                resultMsg = ResponseMessage.error(ResultStatusCode.INVALID_CLIENTID.getErrorMsg(),
                        ResultStatusCode.INVALID_CLIENTID.getErrorCode());
                return resultMsg;
            }

            //验证码校验在后面章节添加

            //验证用户名密码
            Admin user = userService.findUserInfoByName(loginPara.getUserName());
            if (user == null)
            {
                resultMsg = ResponseMessage.error("用户不存在",400);
                return resultMsg;
            }
            else
            {
                String md5Password = EncryptionUtil.MD5 (loginPara.getPassword()+user.getSalt());

                if (md5Password.compareTo(user.getPassword()) != 0)
                {
                    resultMsg = ResponseMessage.error("账号或者密码错误",400);
                    return resultMsg;
                }
            }

            //拼装accessToken
            String accessToken = JwtHelper.createJWT(loginPara.getUserName(), String.valueOf(user.getUsername()),
                    "user", jwtInfo.getClientId(), jwtInfo.getName(),
                    jwtInfo.getExpiresSecond() * 1000, jwtInfo.getBase64Secret());

            //返回accessToken
            AccessToken accessTokenEntity = new AccessToken();
            accessTokenEntity.setAccess_token(accessToken);
            accessTokenEntity.setExpires_in(jwtInfo.getExpiresSecond());
            accessTokenEntity.setToken_type("bearer");
            resultMsg = ResponseMessage.ok(accessTokenEntity);
            return resultMsg;

        }
        catch(Exception ex)
        {
            logger.error("获取info"+ex.getMessage());

            resultMsg = ResponseMessage.error(ResultStatusCode.INVALID_CLIENTID.getErrorMsg(),
                    ResultStatusCode.INVALID_CLIENTID.getErrorCode());
            return resultMsg;
        }
    }


}
